//
//  QCProtectController.swift
//  爱汽车
//
//  Created by liujianlong on 2023/8/25.
//

import UIKit

class QCProtectController: QCViewController {
    var footer : QCProtectFooter!
    var array = ["高压保护","低压保护"]
    var values : [Any] = [true,0]
    var limit = (max:120,min:100)
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    override func initUI() {
        title = LS("电瓶保护")
        
        footer = QCProtectFooter.loadFromXIB() as? QCProtectFooter        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(QCSettingCell.nib(), forCellReuseIdentifier: QCSettingCell.ID())
        tableView.register(QCProtectFooter.nib(), forHeaderFooterViewReuseIdentifier:QCProtectFooter.ID())
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    
    override var bytes: [UInt8]{
        didSet{
            let ack = bytes[7]
            if ack != UInt8(AIQC.userInfo.ack){return}
            let data = [UInt8](bytes[AI_index..<bytes.count])
            if recived == false {
                recived = true
                return
            }
            values[0] = data[2] == 1
            values[1] = Int(data[3])
            tableView.reloadData()
        }
    }
    
}
//MARK: - UITableViewDataSource
extension QCProtectController:UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: QCSettingCell.ID()) as! QCSettingCell
        cell.title.text = LS(array[indexPath.row])
        cell.type = .switchBtn(values[0] as! Bool)
        cell.switchBtn.isHidden = indexPath.row == 1
        cell.block = {[weak self] ison in
            self?.recived = false
            BLEData.appSetDataToPer(AIQC.setBatteryHight.cmd, ison ? "01" : "00").write
        }
        return cell
    }
}
//MARK: - UITableViewDelegate
extension QCProtectController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 80
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        footer.slider.maximumValue = Float(limit.max)
        footer.slider.minimumValue = Float(limit.min)
        footer.value.text = String(format: "%.1fV", CGFloat(values[1] as? Int ?? 0) / 10.0)
        footer.slider.value = Float(values[1] as? Int ?? 0)
        footer.block = {[weak self] value in
            self?.recived = false
            BLEData.appSetDataToPer(AIQC.setBatteryLow.cmd, String(format: "%.2X", UInt8(value))).write
        }
        return footer
    }
}
